/******************************************************************
 * @version      : 1.0
 * @Author       : zhl
 * @Date         : 2023-09-13 15:28:07
 * @LastEditors  : zhl
 * @E-Mail       : zhll168@163.com
 * @Description  : 用于处理实时数据到北向698的映射
 ******************************************************************/
#include "MappingDataManager.h"
#include "DataCenterManager.h"
#include "CDBManager.h"
#include "dlt698_global.h"
#include "CLogger.h"

extern MappingDataManager MappingDataManager::c_mdm;


MappingDataManager::MappingDataManager(/* args */){
}

MappingDataManager::~MappingDataManager(){
}

MappingDataManager& MappingDataManager::getInstance(){
    return c_mdm;
}

std::list<MAPPING_POINT_DATA_TYPE>& MappingDataManager::get_value_by_oad(std::list<MAPPING_POINT_DATA_TYPE>& mapping_datas, std::string oad){
    if(g_dlt698_config.expired()){
        return mapping_datas;
    }
    std::vector<DLT698_MAPPING_POINT_TYPE> mapping_points = CDBManager::getInstance()->query_dlt698_mapping_points(oad);
    if(mapping_points.empty()){
        return mapping_datas;
    }
    std::list<int64_t> dev_ids;
    bool is_query_dev = true;
    for(auto mapping_point = mapping_points.begin(); mapping_point != mapping_points.end(); mapping_point++){
        is_query_dev = true;
        for(auto dev_id = dev_ids.begin(); dev_id != dev_ids.end(); dev_id++){
            if(*dev_id == mapping_point->terminal_id){
                is_query_dev = false;
            }
        }
        if(is_query_dev){
            dev_ids.push_back(mapping_point->terminal_id);
        }
    }
    T_MAPPING_DATA_TYPE mapping_devs_datas;
    if(0 == DataCenterManager::getInstance()->dc_query_realtime_datas(dev_ids, mapping_devs_datas)){
        return mapping_datas;
    }
    for(auto mapping_point = mapping_points.begin(); mapping_point != mapping_points.end(); mapping_point++){
        auto mapping_dev_datas = mapping_devs_datas.find(mapping_point->terminal_id);
        if(mapping_dev_datas == mapping_devs_datas.end()){
            continue;
        }
        auto mapping_dev_data = mapping_dev_datas->second.find(mapping_point->point_id);
        if(mapping_dev_data == mapping_dev_datas->second.end()){
            continue;
        }
        mapping_datas.push_back(mapping_dev_data->second);
    }
    return mapping_datas;
}